package com.PVZ.view.canvas;

import com.PVZ.engine.AnimeControl;
import com.PVZ.engine.Stage;
import com.PVZ.model.Entity;
import com.PVZ.model.ImageEntity;
import com.PVZ.model.plant.Plant;
import com.PVZ.model.zombie.NormalZombie;
import com.PVZ.model.zombie.Zombie;
import com.PVZ.view.GameFrame;
import lombok.Getter;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 * Created by @Author: kabuda on @Date: 2021/5/17 22:40
 * <p>
 * 画布类 所有图像都在其上绘制
 */
@Getter
@Setter
public class EntityCanvas extends JPanel {
    //图像缓冲区
    private Image image;
    private volatile ArrayList<Entity> imageEntities = new ArrayList<>();

    public EntityCanvas() {

        System.out.println("Canvas初始化");

        //显示png图片
        this.setOpaque(false);
        this.setSize(GameFrame.WIDTH, GameFrame.HEIGHT);

        // //初始化缓冲图片
        this.initImage();

    }

    /**
     * 绘制图形缓冲区内容
     */
    public void initImage() {

        //创建缓冲区对象
        image = new BufferedImage(GameFrame.WIDTH, GameFrame.HEIGHT, BufferedImage.TYPE_4BYTE_ABGR_PRE);
        //获取画笔
        // graphics = image.getGraphics();

        /* 绘制缓冲区内容 */
    }

    public void addImage(Entity entity) {
        this.imageEntities.add(entity);
    }

    /**
     * 在面板里绘制背景
     *
     * @param graph 绘制工具
     */
    @Override
    public void paint(Graphics graph) {
        super.paint(graph);

        //不能用增强for否则线程不安全
        for (int i = 0; i < imageEntities.size(); i++) {
            Entity entity = imageEntities.get(i);

            if (entity != null&&entity.getPoint()!=null) {
                Image entityImage = new ImageIcon(entity.getImgSrc()).getImage();
                graph.drawImage(entityImage, (entity.getPoint().x + entity.getReviseX()), (entity.getPoint().y + entity.getReviseY()), this);

            }
        }

    }
}
